package com.my.consumer.config.feign;

import com.my.common.CommonConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/**
 * @author zhaodongchao
 * @date 2020/3/15 17:04
 **/
public class RequestHeaderInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        //获取HttpServletRequest请求头中的身份令牌，添加到feign请求头中
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        String token = attributes.getRequest().getHeader(CommonConstants.TOKEN_KEY) ;
        if(null == token || token.isEmpty()){
            token = "#" ;
        }
        template.header(CommonConstants.TOKEN_KEY,token);
    }
}
